﻿#region Using directives
using System;
using System.Text;
using System.Data;
using System.Collections;
using WP.RM.DataEntity;
using WP.RM.DataAccess;
using WP.RM.BusinessRule;
using WP.DB;
using WP.Commons;
#endregion

namespace WP.RM.BusinessLogical
{
    public class RMCategorySetBOMHdrBLL : IBusinessLogical
    {
         RMCategorySetBOMHdrDAL  rmCategorySetBOMHdrDAL = new RMCategorySetBOMHdrDAL();

         public RMCategorySetBOMHdrBLL()
        {
        }

        #region For implement interface

        #region Select Record by RECID OverLoad +1
         public RMCategorySetBOMHdrDEL SelectDataByRecID(DBAccess dBAccess, string strRecId)
        {
            RMCategorySetBOMHdrDEL rmCategorySetBOMHdrDEL = new RMCategorySetBOMHdrDEL();

            DataFunctions.SelectByRecID(dBAccess, rmCategorySetBOMHdrDEL, rmCategorySetBOMHdrDAL.Fields, strRecId);

            return rmCategorySetBOMHdrDEL;
        }
        public IDataEntity SelectDataByRecID(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            RMCategorySetBOMHdrDEL rmCategorySetBOMHdrDEL = null;

            try
            {
                rmCategorySetBOMHdrDEL = SelectDataByRecID(dBAccess, strRecId);
                return rmCategorySetBOMHdrDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

        #region public bool Insert()
        public bool Insert(IDataEntity iRMCategorySetBOMHdrDEL)
        {
            bool bReturn = true;
            RMCategorySetBOMHdrDEL rmCategorySetBOMHdrDEL = iRMCategorySetBOMHdrDEL as RMCategorySetBOMHdrDEL;
            DBAccess dBAccess = null;

            dBAccess = new DBAccess(false, true);
            try
            {
                bReturn = Insert(dBAccess, rmCategorySetBOMHdrDEL);

                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                bReturn = false;
            }
            return bReturn;
        }
        public bool Insert(DBAccess dBAccess, RMCategorySetBOMHdrDEL  rmCategorySetBOMDEL)
        {
            bool bFlag = true;
            RMCategorySetBOMHdrBRL rmCategorySetBRL = new RMCategorySetBOMHdrBRL();

            if (rmCategorySetBRL.InsertValid(dBAccess, rmCategorySetBOMDEL))
            {
                DataFunctions.InsertTable(dBAccess, rmCategorySetBOMHdrDAL.Fields, rmCategorySetBOMDEL);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }
        #endregion

        #region public bool Delete()
        public bool Delete(string strRecId, byte[] btOldRecStamp)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            bool bFlag = false;

            try
            {
                if (new RMCategorySetBOMHdrBRL().DeleteValid(dBAccess, strRecId, btOldRecStamp))
                {

                    string strSQL = "";
                    strSQL = "select categoryname,setname from rmCategorySetBOMHdr where recid='" + strRecId + "'";
                    DataRow drRow = dBAccess.GetDataFirstRow(strSQL);
                    if (drRow != null)
                    {
                        string strCategoryName = CvtFunc.ToString(drRow["CategoryName"]);
                        string strSetName = CvtFunc.ToString(drRow["SetName"]);

                        RMCategorySetBOMHdrBRL rmCategory = new RMCategorySetBOMHdrBRL();
                        strSQL = " select * from rmCategorySetBOMDetail where CategoryName='" + strCategoryName + "' and SetName='" + strSetName + "'";
                        DataTable dtDetail = dBAccess.GetDataTable(strSQL);
                        foreach (DataRow drDtl in dtDetail.Rows)
                        {
                            rmCategory.DeleteDetail(dBAccess, CvtFunc.ToString(drDtl["RecID"]));
                        }
                        strSQL = "delete from rmCategorySetBOMDetail where CategoryName='" + strCategoryName + "' and SetName='" + strSetName + "'";
                        dBAccess.ExecuteSql(strSQL);

                        strSQL = "select * from rmCategorySetBOMLabourCost where CategoryName='" + strCategoryName + "' and SetName='" + strSetName + "'";
                        DataTable dtLabourCost = dBAccess.GetDataTable(strSQL);
                        foreach (DataRow drDtl in dtLabourCost.Rows)
                        {
                            rmCategory.DeleteDetail_LabourCost(dBAccess, CvtFunc.ToString(drDtl["RecID"])); 
                        }
                        strSQL = "delete from rmCategorySetBOMLabourCost where CategoryName='" + strCategoryName + "' and SetName='" + strSetName + "'";
                        dBAccess.ExecuteSql(strSQL);

                        DataFunctions.AddLog( dBAccess, "删除了类别了 "+ strCategoryName +" ; 系列 "+  strSetName  +" BOM单" , "RM","");
                    }
                    DataFunctions.DeleteByRecID(dBAccess, "rmCategorySetBOMHdr", strRecId);

                    
                    dBAccess.CommitTranAndCloseCon();

                    bFlag = true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
            }

            return bFlag;
        }
        #endregion

        #region public bool Update()
        public bool Update(IDataEntity iRMCategorySetBOMHdrDEL)
        {
            bool bReturn = true;
            RMCategorySetBOMHdrDEL rmCategorySetBOMHdrDEL = iRMCategorySetBOMHdrDEL as RMCategorySetBOMHdrDEL;
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                bReturn = Update(dBAccess, rmCategorySetBOMHdrDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }

            return bReturn;
        }
        public bool Update(DBAccess dBAccess, RMCategorySetBOMHdrDEL rmCategorySetBOMHdrDEL)
        {
            bool bFlag = true;
            RMCategorySetBOMHdrBRL rmCategorySetBOMBRL = new RMCategorySetBOMHdrBRL();

            if (rmCategorySetBOMBRL.UpdateValid(dBAccess, rmCategorySetBOMHdrDEL))
            {
                DataFunctions.UpdateTable(dBAccess, rmCategorySetBOMHdrDAL.Fields, RMCategorySetBOMHdrDEL.TABLENAME_RMCATEGORYSet, rmCategorySetBOMHdrDEL.Tables[0].Rows[0]);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }
        #endregion

        #endregion

    }
}
